package org.example.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.example.mapper.OrderMapper;
import org.example.mapper.OrderItemMapper;
import org.example.pojo.PageBean;
import org.example.pojo.Order;
import org.example.service.OrderService;
import org.example.util.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    
    @Autowired
    private OrderItemMapper orderItemMapper;

    /**
     * 分页查询订单
     *
     * @param pageNum
     * @param pageSize
     * @param orderNumber
     * @return
     */
    @Override
    public PageBean<Order> list(int pageNum, int pageSize, String orderNumber) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Order> page = orderMapper.list(orderNumber);
        System.out.println(page.getResult());
//        Map<String, Object> map = ThreadLocalUtil.get();
//        String username = (String) map.get("username");
//        for (Order order : page.getResult()) {
//            order.setUsername(username);
//        }
        return new PageBean<>(page.getTotal(), page.getResult());
    }

    /**
     * 添加订单
     *
     * @param order
     */
    @Override
    @Transactional
    public Integer add(Order order) {
        order.setCreateTime(LocalDateTime.now());
        order.setUpdateTime(LocalDateTime.now());
        Map<String,Object> map = ThreadLocalUtil.get();
        order.setUsername((String) map.get("username"));
        order.setOrderNumber(String.valueOf(System.currentTimeMillis()));
        orderMapper.add(order);
        if (order.getOrderDetails() != null && !order.getOrderDetails().isEmpty()) {
            order.getOrderDetails().forEach(item -> {
                item.setOrderId(order.getId());
                double totalPrice = item.getPrice().doubleValue() * item.getQuantity();
                item.setTotalPrice(BigDecimal.valueOf(totalPrice));
                orderItemMapper.insert(item);
            });
        }
        return order.getId();
    }

    /**
     * 修改订单
     * @param order
     */
    @Override
    @Transactional
    public void update(Order order) {
        order.setUpdateTime(LocalDateTime.now());
        orderMapper.update(order);
        if (order.getOrderDetails() != null && !order.getOrderDetails().isEmpty()){
            // 删除原来的订单详情
            orderItemMapper.deleteByOrderId(order.getId());
            // 添加新的订单详情
            order.getOrderDetails().forEach(item -> {
                item.setOrderId(order.getId());
                double totalPrice = item.getPrice().doubleValue() * item.getQuantity();
                item.setTotalPrice(BigDecimal.valueOf(totalPrice));
                orderItemMapper.insert(item);
            });
        }
    }

    /**
     * 删除订单
     * @param id
     */
    @Override
    @Transactional
    public void delete(Integer id) {
        orderItemMapper.deleteByOrderId(id);
        orderMapper.delete(id);
    }


    /**
     * 根据id查询订单
     * @param id
     * @return
     */
    @Override
    public Order getById(Integer id) {
        Order order = orderMapper.getById(id);
        return order;
    }

    /**
     * 批量删除订单
     * @param ids
     */
    @Override
    @Transactional
    public void batchDelete(Integer[] ids) {
        for (Integer id : ids) {
            orderMapper.delete(id);
            orderItemMapper.deleteByOrderId(id);
        }
    }

}
